package com.bdqn.controller;

import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.util.AliyunOSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * 阿里云OSS文件上传
 *
 * @author lilibo
 * @date 2021/10/01
 */
@Api(tags="阿里云OSS文件上传接口")
@RestController
@MultipartConfig
@RequestMapping("/aliyun")
public class AliyunOSSController {

    /**
     * 文件上传
     *
     * @return R
     */
    @ApiOperation("阿里云OSS文件上传")
    @WebLogger("阿里云OSS文件上传")
    @PostMapping("/upload")
    public R upload(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //获取表单普通数据
        String username = request.getParameter("username");
        System.out.println("username: " + username);
        //获取上传的文件
        Part part = request.getPart("upfile");
        //调用工具类进行文件上传操作
        String surl = AliyunOSSUtil.uploadFile(part);
        System.out.println(surl);
        return new R<>(surl);
    }

}
